import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';


class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  // ignore: no_logic_in_create_state
  State<MyHomePage> createState() {
    if (Random().nextBool()) {
      return _GetStateObjectRouteState();
    }
    return _LayoutState();
  }
}

class _GetStateObjectRouteState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("子树中获取State对象"),
        actions: [
          IconButton(
            onPressed: () {
              Get.back();
            },
            icon: const Icon(Icons.ac_unit_sharp),
            color: Colors.black,
          )
        ],
      ),
      body: Center(
        child: Column(
          children: [
            Builder(builder: (context) {
              return ElevatedButton(
                onPressed: () {
                  // 直接通过of静态方法来获取ScaffoldState
                  ScaffoldState _state = Scaffold.of(context);
                  // 打开抽屉菜单
                  _state.openDrawer();
                },
                child: const Text('打开抽屉菜单1'),
              );
            }),
            Builder(builder: (context) {
              return ElevatedButton(
                onPressed: () {
                  // // 查找父级最近的Scaffold对应的ScaffoldState对象
                  ScaffoldState _state = context.findAncestorStateOfType<ScaffoldState>()!;
                  // // 打开抽屉菜单
                  _state.openDrawer();
                },
                child: const Text('打开抽屉菜单2'),
              );
            }),
            ElevatedButton(
                onPressed: () {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content:  Text("我是SnackBar")),
                  );
                },
                child: const Text("显示SnackBar")),
            Builder(builder: (context) {
              return ElevatedButton(
                  onPressed: () {
                    // 直接通过of静态方法来获取ScaffoldState
                    // var _state = Scaffold.of(context);
                    // // // 打开抽屉菜单
                    // _state.openDrawer();
                    var _state = context.findAncestorStateOfType<ScaffoldState>();
                    // 打开抽屉菜单
                    (_state as ScaffoldState).openDrawer();
                  },
                  // 查找父级最近的Scaffold对应的ScaffoldState对象
                  child: const Text("1打开菜单"));
            }),
            Builder(builder: (context) {
              return ElevatedButton(
                  onPressed: () {
                    // 直接通过of静态方法来获取ScaffoldState
                    var _state = Scaffold.of(context);
                    // // 打开抽屉菜单
                    _state.openDrawer();
                  },
                  // 查找父级最近的Scaffold对应的ScaffoldState对象
                  child: const Text("2打开菜单"));
            }),
            Builder(builder: (context) {
              // (context as BuildContext).findAncestorWidgetOfExactType(Scaffold);
              // 在 widget 树中向上查找最近的父级`Scaffold`  widget
              var scaffold = context.findAncestorWidgetOfExactType<Scaffold>();
              // 直接返回 AppBar的title， 此处实际上是Text("Context测试")
              var tit = (scaffold!.appBar as AppBar).title;
              return tit!;
            }),
          ],
        ),
      ),
      drawer: const Drawer(),
    );
  }
}

class _LayoutState extends State<MyHomePage> {
// 注意：使用 GlobalKey 开销较大，如果有其他可选方案，应尽量避免使用它。另外，同一个 GlobalKey 在整个 widget 树中必须是唯一的，不能重复。
  static final GlobalKey<ScaffoldState> _globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _globalKey,
      appBar: AppBar(
        title: Text("${MediaQuery.of(context).size.height.toDouble()}"),
        centerTitle: true,
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        color: Colors.amber,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  Get.showSnackbar(const GetSnackBar(
                    title: "333",
                    message: "333",
                  ));
                  _globalKey.currentState!.openDrawer();
                },
                child: const Text("根据Key状态获取控件"))
          ],
        ),
      ),
      onDrawerChanged: (isOpened) {
        // Get.showSnackbar(GetSnackBar(title: "333",message: "333",));
      },
      drawer: const Drawer(),
    );
  }
}

//自定义组件
class CustomWidget extends LeafRenderObjectWidget {
  const CustomWidget({Key? key}) : super(key: key);

  @override
  RenderObject createRenderObject(BuildContext context) {
    // 创建 RenderObject
    return RenderCustomObject();
  }

  @override
  void updateRenderObject(BuildContext context, RenderCustomObject renderObject) {
    // 更新 RenderObject
    super.updateRenderObject(context, renderObject);
  }
}

class RenderCustomObject extends RenderBox {
  @override
  void performLayout() {
    // 实现布局逻辑
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    // 实现绘制
  }
}
